<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp" %>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="微社区,Q生活" name="keywords"/>
<meta content="微社区,Q生活" name="description"/>
<title>微社区|Q生活</title>
<link rel="stylesheet" href="${ctx}/static/css/Master.css"/>
<script src="${ctx}/static/js/jquery-1.9.0.min.js"></script>

<link href="${ctx}/static/css/validation.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="${ctx}/static/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="${ctx}/static/js/jquery.metadata.js"></script>
<script type="text/javascript" src="${ctx}/static/js/messages_cn.js"></script>

<script src="${ctx}/static/plugins/layer/layer.js"></script>
<%-- 基础js,所有页面都需要引入此js,依赖layer.js --%>
<script src="${ctx}/static/js/common/base.js"></script>
<script src="${ctx}/static/js/common/util.js"></script>
<script type="text/javascript">
$(function(){
	$('#newPassword').focus(function () {
		$('#pwdLevel_1').attr('class', 'ywz_zhuce_hongxian');
		$('#newPassword').keyup();
	});
	$('#newPassword').keyup(function () {
		var __th = $(this);
	
		if (!__th.val()) {
			Primary();
			return;
		}
		if (__th.val().length < 6) {
			Weak();
			return;
		}
		var _r = checkPassword(__th);
		if (_r < 1) {
			Primary();
			return;
		}
		if (_r > 0 && _r < 2) {
			Weak();
		} else if (_r >= 2 && _r < 4) {
			Medium();
		} else if (_r >= 4) {
			Tough();
		}
	});
	function Primary() {
		$('#pwdLevel_1').attr('class', 'ywz_zhuce_huixian');
		$('#pwdLevel_2').attr('class', 'ywz_zhuce_huixian');
		$('#pwdLevel_3').attr('class', 'ywz_zhuce_huixian');
	}
	function Weak() {
		$('#pwdLevel_1').attr('class', 'ywz_zhuce_hongxian');
		$('#pwdLevel_2').attr('class', 'ywz_zhuce_huixian');
		$('#pwdLevel_3').attr('class', 'ywz_zhuce_huixian');
	}
	function Medium() {
		$('#pwdLevel_1').attr('class', 'ywz_zhuce_hongxian');
		$('#pwdLevel_2').attr('class', 'ywz_zhuce_hongxian');
		$('#pwdLevel_3').attr('class', 'ywz_zhuce_huixian');
	}
	function Tough() {
		$('#pwdLevel_1').attr('class', 'ywz_zhuce_hongxian');
		$('#pwdLevel_2').attr('class', 'ywz_zhuce_hongxian');
		$('#pwdLevel_3').attr('class', 'ywz_zhuce_hongxian');
	}
	function checkPassword(pwdinput) {
		var maths, smalls, bigs, corps, cat, num;
		var str = $(pwdinput).val()
		var len = str.length;
	
		var cat = /.{16}/g
		if (len == 0) return 1;
		if (len > 16) { $(pwdinput).val(str.match(cat)[0]); }
		cat = /.*[\u4e00-\u9fa5]+.*$/
		if (cat.test(str)) {
			return -1;
		}
		cat = /\d/;
		var maths = cat.test(str);
		cat = /[a-z]/;
		var smalls = cat.test(str);
		cat = /[A-Z]/;
		var bigs = cat.test(str);
		var corps = corpses(pwdinput);
		var num = maths + smalls + bigs + corps;
	
		if (len < 6) { return 1; }
	
		if (len >= 6 && len <= 8) {
			if (num == 1) return 1;
			if (num == 2 || num == 3) return 2;
			if (num == 4) return 3;
		}
	
		if (len > 8 && len <= 11) {
			if (num == 1) return 2;
			if (num == 2) return 3;
			if (num == 3) return 4;
			if (num == 4) return 5;
		}
	
		if (len > 11) {
			if (num == 1) return 3;
			if (num == 2) return 4;
			if (num > 2) return 5;
		}
	}
	
	function corpses(pwdinput) {
		var cat = /./g
		var str = $(pwdinput).val();
		var sz = str.match(cat)
		for (var i = 0; i < sz.length; i++) {
			cat = /\d/;
			maths_01 = cat.test(sz[i]);
			cat = /[a-z]/;
			smalls_01 = cat.test(sz[i]);
			cat = /[A-Z]/;
			bigs_01 = cat.test(sz[i]);
			if (!maths_01 && !smalls_01 && !bigs_01) { return true; }
		}
		return false;
	}
	
	//异步验证旧密码是否正确
    jQuery.validator.addMethod("checkPwdEqual", function(value, element){
        var result = false;
        // 设置同步
        $.ajaxSetup({
            async: false
        });
        $.get("${ctx}/user/"+value+"/checkPwdEqual", function(data){
            result = data==1?true:false;
        });
        // 恢复异步
        $.ajaxSetup({
            async: true
        });
        return result;
    }, "旧密码不正确");
	
  	//异步验证新密码是否和旧密码相同
    jQuery.validator.addMethod("checkNewPwdEqual", function(value, element){
        var result = false;
        // 设置同步
        $.ajaxSetup({
            async: false
        });
        $.get("${ctx}/user/"+value+"/checkPwdEqual", function(data){
            result = data==1?false:true;
        });
        // 恢复异步
        $.ajaxSetup({
            async: true
        });
        return result;
    }, "新密码与旧密码不能相同");
	
	$("#editForm").validate({
        rules: {
        	oldPassword: {
                required: true,
                checkPwdEqual: true
            },
            newPassword: {
                required: true,
                checkNewPwdEqual: true,
                minlength: 6,
                maxlength: 16
            },
            confirmPwd: {
                required: true,
                equalTo: "#newPassword"
            }
        },
        messages: {
        	confirmPwd: {
                equalTo: "两次输入的密码不一致"
            }
        }
    });
	
});

function submit(){
	var form = $("#editForm");
	if(!form.valid()){
		return false;
	}
	
	$.ajax({    
	    //url:form.attr("action"),//可以不要   
	    data:form.serialize(),    
	    type:"post",    
	    cache:false,    
	    dataType:"json",
	    async:true, //默认为true 异步    
	    success:function(data) {    
	        top.layer.msg("修改密码成功，请重新登录",{},function(){
	        	window.location.href="${ctx}/logout";   
            });
	     }   
	});
}
</script>
</head>

<body>
<section class="boxRight">
<form id="editForm" method="post" >
		<div class="OrgTit">修改密码</div>
		<div class="context">
			<div class="add_firstmenu">
				<dl>
					<dt>
						旧密码
					</dt>
					<dd>
						<input id="oldPassword" name="oldPassword" type="password" class="w1"/>
					</dd>
				</dl>
				<dl>
					<dt>
						新密码
					</dt>
					<dd>
						<input id="newPassword" name="newPassword" type="password" class="w1" /><br><span class="gray font12">提示：密码长度6-16位</span>
					</dd>
				</dl>
				<dl>
					<dt>
						安全级别
					</dt>
					<dd>
						<div class="ywz_zhuce_huixian" id='pwdLevel_1'> </div>
						<div class="ywz_zhuce_huixian" id='pwdLevel_2'> </div>
						<div class="ywz_zhuce_huixian" id='pwdLevel_3'> </div>
					</dd>
				</dl>
				<dl>
					<dt>
						确认密码
					</dt>
					<dd>
						<input id="confirmPwd" name="confirmPwd" type="password" class="w1"/>
					</dd>
				</dl>
				<div class="combtn"><a href="javascript:void(0);" onclick="submit()">确定</a><a href="javascript:void(0);" class="utilReset">重置</a></div>
			</div>
		</div>
</form>	
</section>

</body>
</html>
